<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        .wrap {
            width: 1226px;
            margin: 50px auto;
            position: relative;
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
            position: absolute;
            right: 50px;
            bottom: 20px;
        }

        .nav li {
            width: 10px;
            height: 10px;
            border: 1px solid #333;
            border-radius: 50%;
            margin: 0 5px;
        }

        .active {
            background-color: #6ff;
            color: #fff;
        }

        .part {
            width: 100%;
        }

        .part .part-item {
            display: none;
        }

        .part .part-item img {
            width: 100%;
        }

        .part .show {
            display: block;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="nav">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
        <div class="part">
            <div class="part-item show"><img src="../images/1.webp" alt=""></div>
            <div class="part-item"><img src="../images/2.webp" alt=""></div>
            <div class="part-item"><img src="../images/3.jpg" alt=""></div>
            <div class="part-item"><img src="../images/4.webp" alt=""></div>
        </div>
    </div>
</body>
<script>
    var wrap = document.getElementsByClassName("wrap")[0];
    var nav = document.getElementsByClassName("nav")[0];
    var liList = nav.getElementsByTagName("li");  // [li,li,li,li,li,li]

    var partItems = document.querySelectorAll(".part .part-item");

    var index = 0;  //全局变量  -> 记录轮播的下标
    var timer = null; //全局变量 -> 记录计时器的编号

    // 点击切换  
    for (let i = 0; i < liList.length; i++) {  // i=> 0-5
        let li = liList[i];  //获取对应下标的元素
        li.onclick = function () {
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = ""; // 清除每一个导航的class名
                partItems[j].classList.remove("show");
            }

            liList[i].className = "active";
            partItems[i].classList.add("show");
        }
    }

    // 移入暂停 
    wrap.onmouseover = function () {
        clearInterval(timer);
    }

    // 移出启动
    wrap.onmouseout = function () {
        timer = setInterval(function () {
            index++; // 每隔3s 切换下一张 (总共4张 下标: 0 1 2 3)
            if (index >= partItems.length) { // index>=4
                index = 0;
            }

            // 先清除所有的
            for (var j = 0; j < liList.length; j++) {
                liList[j].className = ""; // 清除每一个导航的class名
                partItems[j].classList.remove("show");
            }

            //对应 下标的导航变活跃和图片显示
            liList[index].className = "active";
            partItems[index].classList.add("show");

        }, 3000);
    }


    // 自动切换  -> 每隔3s 切换下一张
    timer = setInterval(function () {
        index++; // 每隔3s 切换下一张 (总共4张 下标: 0 1 2 3)
        if (index >= partItems.length) { // index>=4
            index = 0;
        }

        // 先清除所有的
        for (var j = 0; j < liList.length; j++) {
            liList[j].className = ""; // 清除每一个导航的class名
            partItems[j].classList.remove("show");
        }

        //对应 下标的导航变活跃和图片显示
        liList[index].className = "active";
        partItems[index].classList.add("show");

    }, 3000);





</script>

</html>